package com.kimstore.dao.impl;

import javax.persistence.Query;

import com.kimstore.bean.Staff;
import com.kimstore.bean.User;
import com.kimstore.dao.StaffDao;

public class StaffDaoImpl extends GenericJpaDaoImpl<Staff, Integer> implements StaffDao {

    @Override
    public Staff findByName(String name) {
        Query query = getEntityManager().createQuery("from Staff staff where staff.name = :name").setParameter("name", name);
        Staff staff = (Staff) getSingleResultOrNull(query);

        return staff;
    }

    @Override
    public boolean existsName(String name) {
        Query query = getEntityManager().createQuery("select count(staff) from Staff staff where staff.name = :name");
        query.setParameter("name", name);
        long count = (long) query.getSingleResult();

        return count > 0;
    }

    @Override
    public boolean exists() {
        Query query = getEntityManager().createQuery("select count(staff) from Staff staff");
        long count = (long) query.getSingleResult();

        return count > 0;    }

    @Override
    public Staff findByUser(User user) {
        Query query = getEntityManager().createQuery("from Staff staff where staff.user = :user").setParameter("user", user);
        Staff staff = (Staff) getSingleResultOrNull(query);

        return staff;    }
}
